Imports System.Collections

Partial Public Class pages_Datas_usercontrols_UCDatas : Inherits UserControl

#Region "Events"
    Public Event ExportClick()
    Public Event ResortClick(ByVal sortBy As String, ByVal descending As Boolean, ByVal pageNumber As Integer)
#End Region

#Region "Members"
    Private m_list As CDataList
#End Region

#Region "Interface"
    Public Sub Display(ByVal datas As CDataList, ByVal columns As List(Of EData), ByVal decisions As List(Of EDecision)) 'Full list, paging done internally
        m_list = datas

        'Show/Hide Columns
        colNumber.Visible = datas.Count > 0
        For Each i As EData In columns
            UCDataHeading(plhHeadings).Display(i)
        Next
        For Each i As EDecision In decisions
            UCDataHeading(plhHeadings).Display(i)
        Next

        Select Case ctrlPaging.SortColumn
            Case String.Empty, btnSortByData.CommandArgument
                btnSortByData.Style.Add("text-decoration", "underline")
        End Select

        'Display
        plh.Controls.Clear() 'Only needed for ajax postbacks
        Dim sorted As IList = Nothing 'Fixes the numbering to reflect a user-sorted list (querystring sortBy)
        Dim page As IList = ctrlPaging.Display(datas, sorted) 'In-Memory paging, also outputs the sorted list
        For Each i As CData In page
            UCData(plh).Display(i, sorted, columns, decisions)
        Next
        ctrlPagingTop.Info.Count = ctrlPaging.Info.Count
    End Sub
#End Region

#Region "User Controls"
    Private Shared Function UCData(ByVal target As Control) As pages_datas_usercontrols_UCData
        Dim ctrl As Control = target.Page.LoadControl(CSitemap.UCData)
        target.Controls.Add(ctrl)
        Return CType(ctrl, pages_datas_usercontrols_UCData)
    End Function
    Private Function UCDataHeading(ByVal target As Control) As pages_datas_usercontrols_UCDataHeading
        Dim ctrl As Control = target.Page.LoadControl(CSitemap.UCDataHeading)
        target.Controls.Add(ctrl)
        AddHandler CType(ctrl, pages_datas_usercontrols_UCDataHeading).Resort, AddressOf btnResort_Click
        Return CType(ctrl, pages_datas_usercontrols_UCDataHeading)
    End Function
#End Region

#Region "Event Handlers"
    Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnExport.Click
        RaiseEvent ExportClick()
    End Sub
    Protected Sub btnResort_Click(ByVal sender As Object, ByVal e As System.EventArgs) 'Handles btnSortByDataDate.Click, btnSortByDataOpen.Click, btnSortByDataHigh.Click, btnSortByDataLow.Click, btnSortByDataClose.Click
        'Toggle descending if necessary
        Dim sortBy As String = CType(sender, LinkButton).CommandArgument
        Dim descending As Boolean = ctrlPaging.IsDescending
        Dim currentSort As String = ctrlPaging.SortColumn
        If Not String.IsNullOrEmpty(currentSort) Then
            If currentSort = sortBy Then descending = Not descending
        End If

        'Bubble up as event, search page will add filter info and redirect
        RaiseEvent ResortClick(sortBy, descending, ctrlPaging.Info.PageIndex + 1)
    End Sub
#End Region

#Region "Paging"
    Public ReadOnly Property Info() As CPagingInfo
        Get
            Return ctrlPaging.Info
        End Get
    End Property
    Public Property QueryString() As String
        Get
            Return ctrlPaging.QueryString
        End Get
        Set(ByVal value As String)
            ctrlPaging.QueryString = value
        End Set
    End Property
    Public Property PageSize() As Integer
        Get
            Return ctrlPaging.PageSize
        End Get
        Set(ByVal value As Integer)
            ctrlPaging.PageSize = value
        End Set
    End Property
#End Region

End Class
